package cn.stylefeng.guns.modular.business.frequency.ordertake.controller;

import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.pojo.page.AndaPageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.param.OrderDTO;
import cn.stylefeng.guns.modular.business.frequency.ordertake.model.param.TakeDTO;
import cn.stylefeng.guns.modular.business.frequency.ordertake.model.result.PkgOrderDelivery;
import cn.stylefeng.guns.modular.business.frequency.ordertake.service.OrderTakeService;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.KeyPairGenerator;
import java.util.List;

/**
 * tt_order_take 控制器
 *
 * @author: yujie
 * @date: 2023/05/09 17:16
 */
@Api(tags = "到货管理")
@RestController
@RequestMapping("/api/delivery")
@Slf4j
public class OrderTakeController {

    @Resource
    private OrderTakeService orderTakeService;

    @Resource
    private BusinessUtil businessUtil;


    @ApiOperation(value = "交付待处理列表")
    @GetMapping("/wait")
    public ResponseData getWaitTakeCarOrderList(TakeDTO orderDTO) {

        // 排序
        if (StringUtils.isBlank(orderDTO.getColumn())) {
            orderDTO.setColumn("orderId");
        }
        getDataScopeParameter(orderDTO);
        Page<PkgOrderDelivery> list = orderTakeService.getWaitTakeCarOrderList(orderDTO);
        return ResponseData.andaSuccess(new AndaPageResult<>().AndaPageResult(list, orderDTO));
    }

    private void getDataScopeParameter(OrderDTO orderDTO) {
        //// 当前登录用户最高数据权限
        //String maxDataScope = SecurityUtil.getCurrentUserMaxDataScope();
        //JwtUserDTO user = (JwtUserDTO) SecurityUtil.getCurrentUser(1);
        //if (!maxDataScope.equals(DataScopeEnum.ALL.getValue())) {
        //    if (user.getUser().getDept().getHasSite() == 1) {
        //        // 网点
        //        orderDTO.setSiteId(user.getUser().getDept().getSiteId());
        //        orderDTO.getDeptIds().add(user.getUser().getDept().getId());
        //    } else {
        //        orderDTO.setUserId(user.getUser().getId());
        //    }
        //}
        Long siteId = businessUtil.getSiteId();
        if (siteId == null) {
            List<Long> empByOrgList = businessUtil.getEmpByOrgList();
            orderDTO.setDataList(empByOrgList);
        } else {
            orderDTO.setSiteId(siteId);
            orderDTO.setDispatcherId(LoginContextHolder.me().getSysLoginUserId().toString());
        }


    }


}
